package ldkxingzhe.l365calendar.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.utils.OSUtil;

/**
 * 日程类型fragment
 * Created by ke on 16-1-29.
 */
public class ScheduleCategoryFragment extends AllEditFragmentBase{
    @SuppressWarnings("unused")
    private static final String TAG = "ScheduleCategoryFragment";

    public static final String RESULT = "result";

    private static final String[] names = {"生活", "生日", "工作", "学习", "娱乐"};
    private GridView gridView;
    private BaseAdapter adapter;
    private int selectPosition = -1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_schedule_category, container, false);
        gridView = (GridView) resultView.findViewById(R.id.grid_view);
        adapter = new CategoryAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setIsChanged(true);
                selectPosition = position;
                adapter.notifyDataSetChanged();
            }
        });
        return resultView;
    }

    @Override
    public void onSaveClick() {
        Intent intent = new Intent();
        intent.putExtra(RESULT, names[selectPosition]);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }

    class CategoryAdapter extends BaseAdapter{

        private int cellWidth = -1;

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_category, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.layout = convertView.findViewById(R.id.layout);
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(viewHolder);
                if(cellWidth == -1){
                    cellWidth = OSUtil.getScreenWidth() / gridView.getNumColumns();
                }
                convertView.setMinimumHeight(cellWidth);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText(names[position]);
            viewHolder.imageView.setImageResource(R.drawable.calendar_nav_item_loading);
            if(selectPosition == position){
                viewHolder.layout.setBackgroundColor(Color.GRAY);
            }else{
                viewHolder.layout.setBackgroundColor(Color.WHITE);
            }
            return convertView;
        }
        private class ViewHolder {
            View layout;
            ImageView imageView;
            TextView textView;
        }
    }
}
